/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.nodes.behavior.delegates;

import com.android1.amarena2d.commons.BottomLeftRectF;

public class BoundsDelegateImpl implements BoundsDelegate {

    protected BottomLeftRectF bounds;

    @Override
    public boolean isInBounds(float x, float y) {
        return bounds == null || bounds.contains(x, y);
    }


    @Override
    public float getSaveX(float x, float width) {
        if (bounds == null)
            return x;
        if (x < bounds.left)
            return bounds.left;
        else if (x + width > bounds.right)
            return bounds.right - width;
        else
            return x;
    }

    @Override
    public float getSaveY(float y, float height) {
        if (bounds == null)
            return y;
        if (y < bounds.bottom)
            return bounds.bottom;
        else if (y + height > bounds.top)
            return bounds.top - height;
        else
            return y;
    }

    @Override
    public void setRect(float left, float top, float right, float bottom) {
        if (bounds == null)
            bounds = new BottomLeftRectF();
        bounds.set(left, top, right, bottom);
    }

    @Override
    public void clear() {
        this.bounds = null;
    }

    @Override
    public boolean hasBounds() {
        return bounds != null;
    }

    @Override
    public float getLeft() {
        return bounds.left;
    }

    @Override
    public float getRight() {
        return bounds.right;
    }

    @Override
    public float getTop() {
        return bounds.top;
    }

    @Override
    public float getBottom() {
        return bounds.bottom;
    }
}

